import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BaseTableProps } from '@/components/BaseTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { getDictListPage } from '@/apis/system/dataDict'
import type { IDictParams } from '@/apis/system/dataDict/model/dataDictModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
// import FieldSelect from './components/FieldSelect/index.vue';

export const useModelList = () => {
  // 搜索字段及其他
  const searchForm = reactive<IDictParams>({
    dataType: undefined,
    dataCode: undefined,
    dataValue: undefined,
    // parentId: undefined,
    dataDesc: undefined,
    remark: undefined,
    enableFlag: undefined,
    dataGroup: undefined
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '字典代码',
      field: 'dataCode',
      type: 'input'
    },
    {
      name: '值名称',
      field: 'dataValue',
      type: 'input'
    },
    // {
    //   name: '上下字典关联值',
    //   field: 'parentId',
    //   type: 'selectDict'
    // },
    // {
    //   name: '上下字典关联值',
    //   field: 'parentId',
    //   type: 'custom',
    //   customItem: FieldSelect,
    //   placeholder: '上下字典关联值'
    // },
    {
      name: '备注',
      field: 'remark',
      type: 'input'
    },
    {
      name: '状态',
      field: 'enableFlag',
      type: 'select',
      fixedOption: 'enableOption'
    }
  ])

  // 表格配置
  const tableOptions: BaseTableProps = {
    loadData,
    selection: false,
    immediate: false
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '字典代码',
      field: 'dataCode',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '值名称',
      field: 'dataValue',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    // {
    //   title: '上级字典关联值',
    //   field: 'parentDataValue',
    //   width: 140,
    //   columnSettingConfig: {
    //     enabled: true
    //   }
    // },
    {
      title: '值描述',
      field: 'dataDesc',
      width: 100,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '备注',
      field: 'remark',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '排序',
      field: 'sort',
      width: 70,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '状态',
      field: 'status',
      slot: 'statusSlot',
      width: 80,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '更新人/更新时间',
      field: 'updateBy',
      slot: 'updateBySlot',
      width: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人/创建时间',
      field: 'createBy',
      slot: 'createBySlot',
      width: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 100,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  function loadData(parameter: BasicPagingListParams) {
    const params = handleParams()
    Object.assign(params, parameter)
    return getDictListPage(params)
  }
  const handleParams = (): IDictParams => {
    let params: IDictParams = {
      dataType: searchForm.dataType,
      dataCode: searchForm.dataCode,
      dataValue: searchForm.dataValue,
      dataGroup: searchForm.dataGroup,
      // parentId: searchForm.parentId?.length ? searchForm.parentId[1] : '',
      dataDesc: searchForm.dataDesc,
      remark: searchForm.remark,
      enableFlag: searchForm.enableFlag
    }
    params = removeUndefinedParams(params)
    return params
  }
  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
